package cn.fintecher.pangolin.service.manage.service;

import cn.fintecher.pangolin.common.exception.BadRequestException;
import cn.fintecher.pangolin.common.model.UserModel;
import cn.fintecher.pangolin.common.utils.BeanUtils;
import cn.fintecher.pangolin.common.utils.ZWDateUtil;
import cn.fintecher.pangolin.entity.mysql.SysParam;
import cn.fintecher.pangolin.service.manage.repository.SysParamRepository;
import cn.fintecher.pangolin.service.manage.request.FindSysParamRequest;
import cn.fintecher.pangolin.service.manage.request.ModifySysParamRequest;
import cn.fintecher.pangolin.service.manage.response.SysParamResponse;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Auther: xiaqun
 * @Description:
 * @Date: 18:19 2019/8/14
 */

@Service("sysParamService")
@Transactional(rollbackFor = Exception.class)
public class SysParamService {
    @Autowired
    private BaseService baseService;

    @Autowired
    private SysParamRepository sysParamRepository;

    @Autowired
    private ModelMapper modelMapper;

    /**
     * 修改系统参数
     */
    public void modifySysParam(ModifySysParamRequest request, String token) {
        UserModel userModel = baseService.getUserByToken(token);
        SysParam sysParam = sysParamRepository.findById(request.getId()).orElseThrow(() -> new BadRequestException(null, "sysParam", "sysparam.is.null"));
        BeanUtils.copyPropertiesIgnoreNull(request, sysParam);
        sysParam.setOperatorId(userModel.getId());
        sysParam.setOperateTime(ZWDateUtil.getNowDateTime());
        sysParamRepository.save(sysParam);
    }

    /**
     * 多条件查询系统参数
     */
    public Page<SysParamResponse> findAllSysParam(FindSysParamRequest request, Pageable pageable) {
        Page<SysParam> page = sysParamRepository.findAll(request.getBuilder(), pageable);
        return page.map(e -> modelMapper.map(e, SysParamResponse.class));
    }
}
